#define _CRT_SECURE_NO_WARNINGS
#include "../include/shortcut.h"

void AddShortCut(){
    int a;
	a = 1;
	FILE *fp;
	while(a){
		fflush(stdin);
		fp = fopen(APP_DAT_PATH,"a");

		printf("\n Add Mode: \n	Path:");            //get the shortcut inputted
		fgets(ShortCut[++appcount].Path,260,stdin);
		ShortCut[appcount].Path[strlen(ShortCut[appcount].Path) - 1] = '\0';

		printf("	Name :");
		fgets(ShortCut[appcount].Name,260,stdin);
		ShortCut[appcount].Name[strlen(ShortCut[appcount].Name) - 1] = '\0';

		fprintf(fp,"%s\n",ShortCut[(appcount)].Name);          //write into app.dat
		fprintf(fp,"%s\n",ShortCut[(appcount)].Path);
        fflush(stdin);
		printf("     ---<contiune: y = 1, n = 0>- :  ");
		a = getche() - '0';     //again?
	}
	fflush(stdin);
	fclose(fp);
}

void AntiFill(short length){
	short i;
	for (i = 0; i < length; i++)
		putchar(0);
	for (i = 0; i < length; i++)
		putchar('\b');
}

short AutoFillBack(int length,char EntireStr[],bool WillBack){
	short i,count;
	i = length;
	count = (int)strlen(EntireStr) - i;
	for (; i < count + length; i++)
		putchar(EntireStr[i]);
	if(WillBack)
        for (i = length; i < count + length; i++)
            putchar('\b');
	return count;		//return the number of the char that have been auto filled.
}

void LoadShortCut(){
	FILE *fp;
	if ((fp = fopen(APP_DAT_PATH, "r")) == NULL){
		printf("  !:Cannot find app.dat , now create new one.\n");
        system("md data");
		fp = fopen(APP_DAT_PATH, "w");
	}
	short i;
	fclose(fp);
	fp = fopen(APP_DAT_PATH, "r");

	for (i = 0, appcount = 0; !(feof(fp)); i++)
	{
		fscanf(fp, "%s", ShortCut[i].Name);
		fscanf(fp, "%s", ShortCut[i].Path);
		appcount++;
	}
	fclose(fp);
}

bool DetectChar(char InputChar[], int *ResultIndex){
	uint8 length;
	short i;
	length = strlen(InputChar);
	for (i = 0; i < appcount; i++){
		char tmpstr[32] = { 0 };
		strncat(tmpstr, ShortCut[i].Name, length);
		if (strcmp(tmpstr, InputChar) == 0){
			*ResultIndex = i;
			return true;
		}
	}
	return false;
}
